import json
import random

class userAgentGet:
    def __init__(self,browserType):
        with open(r'code1/user_agent.json','r') as f:
            jsoncode=f.read()
        self.__initStr=json.loads(jsoncode)
        self.browserTypes=self.__initStr['browsers'].keys()
        if browserType not in self.browserTypes:
            raise RuntimeError("浏览器名称输入有误,可以输入的为  'chrome', 'opera', 'firefox', 'internetexplorer', 'safari'")
        self.browerValues=self.__initStr['browsers'][browserType]
    def userAgentGetValue(self):
        return random.choice(self.browerValues)



if __name__=='__main__':
    myuserAgent=userAgentGet('chrome')
    print(myuserAgent.browserTypes)

    print(myuserAgent.userAgentGet())

